package guiTest;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class RatingsRenderer extends JPanel implements TableCellRenderer 
{
	JLabel ratingsLabel;
	int rating;

	public RatingsRenderer()
	{
		ratingsLabel = new JLabel();
		setOpaque(true);
		rating = 0;
	}
	
	public Component getTableCellRendererComponent(JTable table, Object value,
	     boolean isSelected, boolean hasFocus, int row, int column)
	{
		ratingsLabel.setText(value.toString());
		rating = ((Integer)value).intValue();
		
		if(isSelected)
		{
			setBackground(Color.yellow);
		}
		else
		{
			setBackground(Color.white);			
		}
		
		repaint();
		//add(ratingsLabel);
		
		return this;
	}
	
	public void paintComponent(Graphics g) 
	{
		super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();
        g.setColor(Settings.RATING_COLOR);
        g.fillRect(25, 0, ((width - 25) * rating) / 100, height);
        
        g.setColor(Color.black);
        Font f = new Font(Settings.FONT_NAME, Font.PLAIN, 18);
        FontMetrics fm = g.getFontMetrics(f);
        int fontHeight = fm.getAscent();
        g.setFont(f);
        g.drawString("" + rating, 2, (height - fm.getHeight()) / 2 + fm.getAscent());
        
       // System.out.println("height = " + height + ", fh = " + fontHeight);
   }



}
